﻿using System;
using System.Collections.Generic;
using Omniscient.Foundation.Contrib.Data.MySql;
using Omniscient.Foundation.Data;
using MySql.Data.MySqlClient;
using Omniscient.Foundation.ApplicationModel;

namespace Omniscient.TimeTracker.Data.Adapters.MySql
{
    public class TaskAdapter : IEntityAdapter<Task>
    {
        IConnectivity _connectivity;

        public TaskAdapter() : this(ApplicationManager.Current.ServiceProvider.GetService<IConnectivity>()) { }

        public TaskAdapter(IConnectivity connectivity)
        {
            if (connectivity == null) throw new ArgumentNullException("The adapter cannot work with no connectivity.  Null argument: connectivity.");
            _connectivity = connectivity;
        }

        #region IEntityAdapter<Task> Members

        public EntityList<Task> LoadByObjectQuery(Omniscient.Foundation.Data.ObjectQuery.OQuery<Task> query)
        {
            throw new NotImplementedException();
        }

        public Task LoadByKey(Guid id)
        {
            MySqlConnection cn;
            MySqlCommand cmd;
            MySqlDataReader rd;

            cn = _connectivity.CreateConnection();

            string sql = string.Format(@"SELECT ProjectGuid, TaskStatusGuid, Name, Description, IssueNumber, Priority, StartDate, EndDate 
                            FROM task
                            WHERE TaskGuid = '{0}'", id);

            try
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = sql;
                cn.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Task t;
                    t = new Task(id, true);

                    id = new Guid(rd.GetString("ProjectGuid"));
                    t.ParentProject = new Project(id, false);

                    t.Name = rd.GetString("Name");
                    t.Description = rd.GetString("Description");
                    t.IssueNumber = rd.GetInt32("IssueNumber");
                    t.Priority = rd.GetInt32("Priority");
                    t.StartDate = rd.GetDateTime("StartDate");
                    t.EndDate = rd.GetDateTime("EndDate");

                    rd.Close();
                    return t;
                }
                rd.Close();
                return null;
            }
            finally
            {
                cn.Dispose();
            }
        }

        public void Save(Task entity)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEntityAdapter<Task> Members

        public EntityList<Task> LoadAll()
        {
            MySqlConnection cn;
            MySqlCommand cmd;
            MySqlDataReader rd;
            EntityList<Task> tasks;

            tasks = new EntityList<Task>();
            cn = _connectivity.CreateConnection();

            string sql = @"SELECT TaskGuid, ProjectGuid, TaskStatusGuid, Name, Description, IssueNumber, Priority, StartDate, EndDate FROM task";

            try
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = sql;
                cn.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Task t;
                    Guid id;

                    id = new Guid(rd.GetString("TaskGuid"));
                    t = new Task(id, true);

                    id = new Guid(rd.GetString("ProjectGuid"));
                    t.ParentProject = new Project(id, false);

                    id = new Guid(rd.GetString("TaskStatusGuid"));
                    t.TaskStatus = new TaskStatus(id, false);

                    t.Name = rd.GetString("Name");
                    t.Description = rd.GetString("Description");
                    t.IssueNumber = rd.GetInt32("IssueNumber");
                    t.Priority = rd.GetInt32("Priority");
                    t.StartDate = rd.GetDateTime("StartDate");
                    t.EndDate = rd.GetDateTime("EndDate");

                    tasks.Add(t);
                }
                rd.Close();
                return tasks;
            }
            finally
            {
                cn.Dispose();
            }
        }

        public EntityList<Task> LoadByForeignKey(string propertyName, Guid id)
        {
            throw new NotImplementedException();
        }

        public EntityList<Task> LoadByQuery(string queryName)
        {
            throw new NotImplementedException();
        }

        public EntityList<Task> LoadByValueProperty(string propertyName, object value)
        {
            throw new NotImplementedException();
        }

        public void Save(IEnumerable<Task> entities)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
